
if (files.cwd().endsWith("脚本")) {
  let _require = require;
  require = function (path) {
    if (path.startsWith("./")) {
      path = path.replace("./", "");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}
auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");
var time = random(2000, 3000)

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!")
    while (true) {
      var allow = text("允许").findOne(5000)
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!")
        allow.click()
        sleep(1000)
        thread.interrupt()
        break
      }
    }
  })
  sleep(1000)
  var result = launchApp(app_name)
  sleep(3000)
  thread.interrupt()
  return result
}


function task() {
    sleep(3000)
    var kc = text('矿场').findOne(20000)
    if (kc) {
        click(kc.bounds().centerX(),kc.bounds().centerY());
        utils.updateAppProgress(10);
        sleep(1000);
        var 已购买 = text("已购买").findOne(1000)
        if(已购买){
            已购买.click();
            sleep(1000)
            var 领取 = text("领取").findOne(1000);
            if(领取){
              var finalActivity = currentActivity();
              领取.click()
              utils.waitAdvert(finalActivity);
            }
        }
      sleep(1000)
      log("领取完成.")
    }else{
        log("干嘛去了~~~~")
    }
}

function main() {
  let open_app = launchAppEnhance("E支付");
  if (open_app) {
    task()
  }
  else {
    toastLog("E支付打开失败，请确认是否安装并授权打开!");
  }
}

try {
  main();
  utils.updateAppProgress(100);
} catch (error) {
  toastLog(error);
}

storage.put("script_running_status", "end");
